<?php
/**
 * 
 * author Anthony Straetman <anthony.straetman@gmail.com>
 * 
 */
class Bootstrap {

	public $modules;
	public $controller;

	function __construct() {
		
		// Get all modules
		$this->modules = $this->getModules();
		
		// Get url variables
		$url_parameters = $this->getParameters();
		
		// Controller information
		$controller_name = $url_parameters['module'] . '_controller';
		$controller_url = 'library/modules/' . $url_parameters['module'] .  '/controllers/' . $url_parameters['module'] . '_controller.php';
		
		// Load the right controller
		include $controller_url;
		$this->controller = new $controller_name($url_parameters);
	}

	// Get all modules
	function getModules() {
		$modules = scandir('library/modules');
		$modules = array_slice($modules, 2);
		return $modules;
	}
	
	// Store all $_GET parameters from the url as $key = $value
	// If module isn't set, the default module will be loaded (home for now)
	// If module doesn't exist, errormodule will be loaded
	function getParameters() {
		$url = $_SERVER['REQUEST_URI'];
		$parameters;
		
		if(strpos($url, '?')) {
			$url = explode('?', $url);
			$getString = $url[1];
			$urlPars = explode('&', $getString);

			foreach($urlPars as $par) {
				$parSplit = explode('=', $par);
				$parameters[$parSplit[0]] = $parSplit[1];
			}
		}
		
		// Set default module when no module is requested
		if($parameters['module'] == null) {
			$parameters['module'] = 'home'; // TODO: define default module in config
		}
		
		// Set module to errormodule when a non-existing module is requested
		if(!in_array($parameters['module'], $this->modules)) {
			$parameters['module'] = 'error';
		}
		
		return $parameters;
	}

}